/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
import model.Locatie;

/**
 *
 * @author misja
 */
public class LocatieIndex_1 extends javax.swing.JFrame {

    /**
     * Creates new form LocatieIndex
     */
    public LocatieIndex() {

        initComponents();

        if (!Database.openConnection("fullhouse", "root", "")) {
            System.out.println("Geen verbinding met de database");
            System.exit(0);
        }

        this.vulTabelLocatie();
    }

    private void vulTabelLocatie() {
        //Ophalen van de toernooien uit de database

        Locatie locatie = new Locatie();
        String[] kolommen = {"Naam", "Plaats", "Straat", "Postcode", "Huisnummer"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);

        int aantalLocaties = locatie.haalAantalLocatiepOp();

        //Lijstje aanmaken voor de tennistoernooien die we uit de database gaan halen. 
        Locatie[] locaties;
        if (aantalLocaties == -1) {
            //return null;
        } else {
            locaties = new Locatie[aantalLocaties];
        }

        try {

            String sql = "SELECT * FROM locatie";
            ResultSet resultaat = Database.executeSelectQuery(sql);

            int teller = 0;
            while (resultaat.next()) {

                String huisnummer = resultaat.getString("locatie_huisnummer");
                String postcode = resultaat.getString("locatie_postcode");

                Locatie tabelVulling = new Locatie(huisnummer, postcode);
                model.addRow(tabelVulling.getInformatie());



            }


        } catch (SQLException e) {
        }

        tblLocatie.setModel(model);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblLocatie = new javax.swing.JTable();
        btnToevoegen = new javax.swing.JButton();
        btnVerwijderen = new javax.swing.JButton();
        btnBijwerken = new javax.swing.JButton();
        lblLocatie = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tblLocatie.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblLocatie);

        btnToevoegen.setText("toevoegen");
        btnToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToevoegenActionPerformed(evt);
            }
        });

        btnVerwijderen.setText("verwijderen");
        btnVerwijderen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerwijderenActionPerformed(evt);
            }
        });

        btnBijwerken.setText("bijwerken");
        btnBijwerken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBijwerkenActionPerformed(evt);
            }
        });

        lblLocatie.setText("Locaties");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLocatie)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnToevoegen)
                            .addComponent(btnVerwijderen)
                            .addComponent(btnBijwerken))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblLocatie)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnToevoegen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnVerwijderen)
                        .addGap(12, 12, 12)
                        .addComponent(btnBijwerken))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToevoegenActionPerformed
        new LocatieDialoog('t', "", "").setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnToevoegenActionPerformed

    private void btnVerwijderenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerwijderenActionPerformed
        int geselecteerdeRij = tblLocatie.getSelectedRow();
        String huisnummer = (String) tblLocatie.getModel().getValueAt(geselecteerdeRij, 3);
        String postcode = (String) tblLocatie.getModel().getValueAt(geselecteerdeRij, 4);

        new LocatieDialoog('v', huisnummer, postcode).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnVerwijderenActionPerformed

    private void btnBijwerkenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBijwerkenActionPerformed
        int geselecteerdeRij = tblLocatie.getSelectedRow();
        String huisnummer = (String) tblLocatie.getModel().getValueAt(geselecteerdeRij, 3);
        String postcode = (String) tblLocatie.getModel().getValueAt(geselecteerdeRij, 4);

        new LocatieDialoog('b', huisnummer, postcode).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnBijwerkenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LocatieIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LocatieIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LocatieIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LocatieIndex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LocatieIndex().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBijwerken;
    private javax.swing.JButton btnToevoegen;
    private javax.swing.JButton btnVerwijderen;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblLocatie;
    private javax.swing.JTable tblLocatie;
    // End of variables declaration//GEN-END:variables
}
